package jnpf.engine.custom.type;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jnpf.engine.entity.FlowTaskOperatorRecordEntity;
import jnpf.engine.enums.FlowRecordEnum;
import jnpf.engine.service.FlowTaskOperatorRecordService;
import jnpf.engine.service.FlowTaskOperatorService;
import jnpf.engine.util.BpmnModelUtils;
import jnpf.engine.util.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.flowable.bpmn.model.Process;
import org.flowable.bpmn.model.UserTask;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.engine.delegate.event.impl.FlowableEntityEventImpl;
import org.flowable.task.service.impl.persistence.entity.TaskEntityImpl;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Doctor4JavaEE
 * @since 2024/2/18
 */
@Component
@Slf4j
public class TaskDeletedListener  implements FlowableEventListener {
    @Override
    public void onEvent(FlowableEvent event) {
        if(event instanceof FlowableEntityEventImpl){{
            FlowableEntityEventImpl flowableEntityEvent =(FlowableEntityEventImpl) event;
            Object entity = flowableEntityEvent.getEntity();
            //
            if(entity instanceof TaskEntityImpl){
                //
                TaskEntityImpl task = (TaskEntityImpl) entity;
                if(StringUtils.isNotBlank(task.getProcessDefinitionId())){
                    Process bpmnModelMainProcess = BpmnModelUtils.getBpmnModelMainProcess(task.getProcessDefinitionId());
                    UserTask firstNode = BpmnModelUtils.getFirstNode(bpmnModelMainProcess);
                    String id = firstNode.getId();
                    if(!id.equals(task.getTaskDefinitionKey())){
                        FlowTaskOperatorRecordService flowTaskOperatorRecordService = SpringContextHolder.getBean(FlowTaskOperatorRecordService.class);
                        LambdaQueryWrapper<FlowTaskOperatorRecordEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                        lambdaQueryWrapper.eq(FlowTaskOperatorRecordEntity::getTaskNodeId,task.getId());
                        lambdaQueryWrapper.eq(FlowTaskOperatorRecordEntity::getHandleStatus,FlowRecordEnum.DOING.getCode());
                        //转办的情况下 是2
                        List<FlowTaskOperatorRecordEntity> list = flowTaskOperatorRecordService.list(lambdaQueryWrapper);
                        for (FlowTaskOperatorRecordEntity flowTaskOperatorRecordEntity : list) {
                            flowTaskOperatorRecordEntity.setHandleStatus(FlowRecordEnum.COMPLETE.getCode());
                        }
                        flowTaskOperatorRecordService.updateBatchById(list);

                    }
                    log.error("任务被删除进入");

                }
            }
        }

        }
    }

    @Override
    public boolean isFailOnException() {
        return true;
    }

    @Override
    public boolean isFireOnTransactionLifecycleEvent() {
        return false;
    }

    @Override
    public String getOnTransaction() {
        return null;
    }
}
